
package com.mengmeizi.adapter;

import com.mengmeizi.bean.ImageDataBean;
import com.mengmeizi.util.Const;
import com.mengmeizi.util.ImageManager;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import java.util.ArrayList;

public class MainImagesAdapter extends BaseAdapter {

    private Context context;

    private Handler handler;

    private ImageManager manager;

    private ArrayList<ImageDataBean> urlList;

    public MainImagesAdapter(Context context, Handler handler, ArrayList<ImageDataBean> urlList) {
        this.context = context;
        this.handler = handler;
        this.urlList = urlList;

        manager = ImageManager.getManger();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (urlList != null) {
            return urlList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        if (urlList != null) {
            return urlList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView == null) {
            convertView = new ImageView(context);
            convertView.setVisibility(View.GONE);
            convertView.setBackgroundColor(0xfff);
        }

        String url = urlList.get(position).thumbUrl;
        if (url != null && url.startsWith("http://")) {
            manager.loadImage2ImageView(context, url, (ImageView) convertView, Const.TYPE_SMALL_IMAGE, handler);
        }

        return convertView;
    }

}
